const express = require("express");
const authMiddleware = require("../middlewares/server.authorization.middle.js");
const api = require("../modules/api.js");
var multer  = require('multer')
//var upload = multer({ dest: 'uploads/' });
const path = require("path");
const fs = require("fs");
const request = require("request");
const router = express.Router();

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'uploads/')
  },
  filename: function (req, file, cb) {
    console.log("----ok-----");
    console.log(file)
    cb(null, file.originalname)
  }
});
 
var upload = multer({ storage: storage })


/*---------首页路由--------*/
router.get("/", (req,res,next)=>{
    Promise.all([
        api.home_banner.bind(req)(),
        api.product_query.bind(req)({pageNo: 1, pageSize: 7, isTPOnly: true}),
        api.product_query.bind(req)({pageNo: 1, pageSize: 7, isTPOnly: false})
    ]).then((data)=>{
        let banners = data[0].result.all;
        let tiepai_list = data[1].result.list || [];
        let daili_list = data[2].result.list || [];
        let list = [1,2,3,4,5,6,7,8,9,10,12];
        let title = "万能工厂 — 首页";
        res.render("index.html", {req, banners, list, tiepai_list, daili_list, title});
    });
});

/*--------产品详情----------*/
router.get("/product/detail/:id", (req, res, next)=>{
    Promise.all([
        api.product_detail.bind(req)(req.params.id),
        api.product_recommend.bind(req)({productId: req.params.id, isTPOnly: true, pageNo: 1, pageSize: 12}),
        api.product_orderRecode.bind(req)({productId: req.params.id, pageNo: 1, pageSize: 10}),
        api.product_rateList.bind(req)({productId: req.params.id, pageNo: 1, pageSize: 10})
    ]).then(data=>{
        let product = data[0].result || {};
        let list = data[1].result || [];

        let orderRecode_list = data[2].result.list || [];
        let orderRecode = data[2].result.page.totalRecords;
        let recodePage = data[2].result.page;


        let rateList = data[3].result.list || [];
        let rates = data[3].result.page.totalRecords;
        let retePage = data[3].result.page;
        
        let params = [];
        try{
            params = JSON.parse(product.params);
        }catch(e){
            console.log(e);
        };

        let descs = [];
        let lines = Math.ceil(params.length / 3);
        console.log(lines);

        for(let i = 0; i < lines; i ++){
            let arr = [];
            for(let j = 0; j < 3; j ++){
                arr.push(params[ i * 3 + j]);
            }
            descs.push(arr);
        };
        
        let pageNo = 1;
        let base_url = req.path;
        let title = "万能工厂 - 商品详情";
        res.render("product/detail.html", {
            req, 
            product, 
            list, 
            orderRecode_list, 
            rateList, 
            orderRecode, 
            rates, 
            descs, 
            recodePage,
            retePage,
            base_url,
            pageNo,
            title
        });
    });
});

/*--------登录页面-----------*/
router.get("/login", (req,res,next)=>{
    let title = "万能工厂 - 登录";
    res.render("login.html", {req, title});
});

/*--------订单确认页面-----------*/
router.get("/order/confirm", authMiddleware.login_required, (req,res, next)=>{
    Promise.all([
        api.address_myList.bind(req)()
    ]).then(data=>{
        console.log(data);
        let addresses = data[0].result.list || [];

        console.log(addresses);
        let title = "万能工厂 - 下单";
        res.render("order/confirm.html", {req, addresses, title});
    });
});

/*--------通用成功页面-----------*/
router.get("/success", (req, res, next)=>{
    let title = "万能工厂";
    res.render("success.html", {req, title});
});

/*--------工厂入驻页面------------*/
router.get("/factory/entry", (req, res, next)=>{
    let title = "万能工厂 - 工厂入驻";
    res.render("factory/entry.html", {req, title});
});

/*--------工厂入驻确定页面----------*/
router.get("/factory/entry/confirm", (req, res, next)=>{
    let type = req.query.type || "COMMON";
    console.log(type);
    let title = "万能工厂 - 工厂入驻";
    res.render("factory/entry_confirm.html", {req, type, title});
});


/*---------批发代理页面-------------*/
router.get("/product/pf", (req,res, next)=>{
    let pageNo = parseInt(req.query.pageNo || 1);
    Promise.all([
        api.product_query.bind(req)({pageNo: pageNo, pageSize: 12, isTPOnly: false})
    ]).then(data=>{
        let list = data[0].result.list || [];
        let page = data[0].result.page;
        let base_url = req.path;
        console.log(base_url);
        let title = "万能工厂 - 批发代理";
        res.render("product/list.html", {req, base_url, list, page, pageNo, title});
    });
});

/*---------贴牌定制页面--------------*/
router.get("/product/tp", (req,res, next)=>{
    let pageNo = parseInt(req.query.pageNo || 1);
    Promise.all([
        api.product_query.bind(req)({pageNo, pageSize: 12, isTPOnly: true})
    ]).then(data=>{
        let list = data[0].result.list || [];
        let page = data[0].result.page;
        let base_url = req.path;
        console.log(base_url);
        let title = "万能工厂 - 贴牌定制";
        res.render("product/list.html", {req, base_url, list, page, pageNo, title});
    });
    
});

/*---------加工定制页面---------*/
router.get("/process", authMiddleware.login_required, (req, res, next)=>{
    let title = "万能工厂 - 加工定制";
    res.render("process.html", {req, title});
});


/*---------退出登录-------*/
router.get("/user/logout", authMiddleware.login_required, (req, res, next)=>{
    api.user_logout.bind(req)().then(data=>{
        if(data.code == "0"){
            res.clearCookie("token");
            res.clearCookie("uid");
            res.redirect("/");
        };
    });
});


module.exports = router;